 package view;

import java.sql.*;

public class DBConnect {
	
	private static String urlLead = "jdbc:oracle:thin:@//";
	private static String dbURL = null;
	private static String dbDriverName = "oracle.jdbc.OracleDriver";
	private static String hostName;
	private static String port;
	private static String sid;
	private static String id;
	private static String pwrd;
	private static Connection conn = null;
	private static boolean driverLoaded = false;
	
	public DBConnect(String insHostname, String insPort, String insSid, String insId, String insPwrd) //Moved Connection data into initialization to prevent null classes
    {
		try
        {
          if (!driverLoaded) {
        	loadDriver();
            driverLoaded=true;
         }
        
         setHostName(insHostname);
         setPort(insPort);
         setSid(insSid);
         setId(insId);
         setPwrd(insPwrd);
         setDbURL();
       }
       catch(Exception e)
       {
          System.out.println("Connection Initialization Failed");
          e.printStackTrace();
       }
		return;
    }
	
	private void loadDriver()
    {
    	
    	try
    	{
    		Class.forName(dbDriverName);
    	}
    	catch(Exception e)
    	{
    		System.out.println("Driver could not be loaded");
    		System.out.println("/nMessage: " + e.getMessage());
    		e.printStackTrace();
    	}
    return;
    }
	
	public Connection getConnection() //Used to get a connection as well as activate the connection itself
    {
   	
    	try
        {
	           if (conn != null)
	        	   if(conn.isValid(5))
	        	   {
	        		   System.out.println("Connected");
	        		   return conn;
	        	   }   
	           
         conn=DriverManager.getConnection(dbURL, id, pwrd);
         conn.setAutoCommit(false);
        
         System.out.println("Connected");
       }
       catch(SQLException e)
       {
    	 System.out.println("Error in Create Connnection" + e.getSQLState());
         System.out.println("/nError Code: " + e.getErrorCode());
         System.out.println("/nMessage: " + e.getMessage());
       }	
       catch(Exception e)
       {
          System.out.println("Not Connected");
          e.printStackTrace();
       }
         return conn;
    }
	
	public void Disconnect() //Disconnects from database, but does not destroy instance
	{
		try
		{
		
			if(conn!=null)
				conn.close();
		}
	       catch(SQLException e)
	       {
	    	 System.out.println("Error in Create Connnection" + e.getSQLState());
	         System.out.println("/nError Code: " + e.getErrorCode());
	         System.out.println("/nMessage: " + e.getMessage());
	       }	
	       catch(Exception e)
	       {
	          System.out.println("Not Connected");
	          e.printStackTrace();
	       }
	}
	
	/* All sets are private, all gets are public */
	
	public static String getDbURL() {
		return dbURL;
	}

	private void setDbURL() {
		dbURL = urlLead + hostName + ':' + port + '/' + sid;
	}

	public static String getDbDriverName() {
		return dbDriverName;
	}

	public static String getHostName() {
		return hostName;
	}

	private void setHostName(String inHostName) {
		hostName = inHostName;
	}

	public static String getPort() {
		return port;
	}

	private void setPort(String inPort) {
		port = inPort;
	}

	public static String getSid() {
		return sid;
	}

	private void setSid(String inSid) {
		sid = inSid;
	}


	public static String getId() {
		return id;
	}

	private void setId(String inId) {
		id = inId;
	}

	public static String getPwrd() {
		return pwrd;
	}

	private void setPwrd(String inPwrd) {
		pwrd = inPwrd;
	}
}
